package it.nard.listener;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;



public class ControlDocumentListener implements DocumentListener {

	public static final String MANDATORY_FIELD = "mandatory";
	public static final String NUMBER_FIELD = "numeric";
	
	private HashMap<Document, DocumentBean> controlElement = new HashMap<Document, DocumentBean>();
	
	public void addControlElement(String type, JTextField originator, ArrayList<JButton> linkedButton, ArrayList<JTextField> mandatoryButtons, ArrayList<JTextField> numericFields){
	
		DocumentBean db = new DocumentBean();
		db.type = type;
		db.linkedButton = linkedButton;
		db.originator = originator;
		db.mandatoryField = mandatoryButtons;
		
		db.setEnableLinkedButton(false);
		
		controlElement.put(db.originator.getDocument(), db);
		
		db.originator.getDocument().addDocumentListener(this);
		
	}
	
	public void insertUpdate(DocumentEvent e) {
		if(e!=null && e.getDocument()!=null)
			doControl(controlElement.get(e.getDocument()));
	}

	public void removeUpdate(DocumentEvent e) {
		if(e!=null && e.getDocument()!=null)
			doControl(controlElement.get(e.getDocument()));
	}

	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		
	}

	
	
	public void doControl (DocumentBean db){
		String originatorText = db.originator.getText();
		
		db.setEnable(false);
		if(db.type.equals(MANDATORY_FIELD)){
			
			if(originatorText!=null && !originatorText.trim().equals("")){
				
				db.setEnable(db.checkMandatory() && db.checkNumeric());
				db.setDefaultColor();
			}
			
		}
		else if(db.type.equals(NUMBER_FIELD)){
			
			if(originatorText!=null && !originatorText.trim().equals("")){
//				try{
//					Double d = Double.valueOf(originatorText.trim());
//					if(d>=0){
//						db.setEnable(db.checkMandatory() && db.checkNumeric());
//						db.setDefaultColor();
//					}
//					else{
//						throw new Exception();
//					}
//				}catch(Exception e ){
//					db.setEnable(false);
//					db.setErrorColor();
//				}
				
				boolean out = originatorText.matches("\\d+(\\.\\d\\d?)?");
				if(out){
					db.setEnable(true);
					db.setDefaultColor();
				}
				else{
					db.setEnable(false);
					db.setErrorColor();
				}
			}
			else{
				db.setEnable(true);
			}
		}
	}
}




class DocumentBean{
	String type;
	JTextField originator;
	ArrayList<JButton> linkedButton;
	ArrayList<JTextField> mandatoryField;
	ArrayList<JTextField> numericField;
	
	
	void setEnableLinkedButton (boolean enable){
		if(linkedButton!=null){
			Iterator<JButton> iter = linkedButton.iterator();
			while(iter.hasNext()){
				JButton elem = iter.next();
				elem.setEnabled(enable);
			}
		}
	}
	
	void setEnable( boolean enable){
		
		setEnableLinkedButton(enable);
		if(enable){
			setDefaultColor();
		}
		else
			setErrorColor();
	}
	
	void setDefaultColor(){
		originator.setBackground(Color.white);
	}
	
	void setErrorColor(){
		originator.setBackground(Color.RED);
	}
	
	
	boolean checkMandatory (){
		boolean out = true;
		if(mandatoryField!=null){
			Iterator<JTextField> iter = mandatoryField.iterator();
			while(iter.hasNext()){
				JTextField elem = iter.next();
				if(!(elem.getText()!=null && !elem.getText().trim().equals(""))){
					out = false;
					break;
				}
			}
		}
		return out;
		
		
	}
	
	
	boolean checkNumeric (){
		boolean out = true;
		if(numericField!=null){
			Iterator<JTextField> iter = numericField.iterator();
			while(iter.hasNext()){
				JTextField elem = iter.next();
				if((elem.getText()!=null && elem.getText().trim().equals(""))){
//					try{
//						
//						Double d = Double.valueOf(elem.getText().trim());
//						if(d<0){
//							out = false;
//							break;
//						}	
//					}catch(Exception e ){
//						out = false;
//						break;
//						
//					}
					
					out = elem.getText().matches("\\d+(\\.\\d\\d?)?");
				
					
					
					
				}
			}
		}
		return out;		
	}
	
}